//
//  AppDelegate.m
//  MiguMetaEntrance
//
//  Created by chenxin on 2024/3/6.
//

#import "AppDelegate.h"
#import "HomeViewController.h"

@interface AppDelegate ()

@property (nonatomic, strong) UIImage *backButtonImage;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    HomeViewController *homeVC = [[HomeViewController alloc] init];
    PTNavigationController *nav = [[PTNavigationController alloc] initWithRootViewController:homeVC];
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];

    [self initUIAppearance];
    return YES;
}

- (void)initUIAppearance {
    /// iOS 15 UITableView顶部空白解决方案
    if (@available(iOS 15.0, *)) {
        UITableView.appearance.sectionHeaderTopPadding = 0.0;
    }

    //顶部栏定制
    [[UINavigationBar appearance] setTintColor:[UIColor mgcNavWordColor]];
    [[UINavigationBar appearance] setBarTintColor:[UIColor mgcBackgroundColor1]];
    [[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor mgcNavWordColor],NSForegroundColorAttributeName,[UIFont mediumPingFangFontOfSize:20],NSFontAttributeName,nil]];
    [[UINavigationBar appearance] setBackgroundImage:[[UIImage imageWithColor:[UIColor mgcBackgroundColor1]] resizableImageWithCapInsets:UIEdgeInsetsMake(1, 1, 1, 1) resizingMode:UIImageResizingModeStretch] forBarMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance] setShadowImage:[UIImage imageWithColor:[UIColor clearColor]]];
    
    //返回按钮定制
    UIImage *backButtonImage = self.backButtonImage;
    [[UINavigationBar appearance] setBackIndicatorImage:backButtonImage];
    [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:backButtonImage];
    [[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont mediumPingFangFontOfSize:15], NSFontAttributeName, nil] forState:UIControlStateNormal];
    
    if (IOS_VERSION < 11) {
        [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -80) forBarMetrics:UIBarMetricsDefault];
    }
    
    [[UITextField appearance] setTintColor:[UIColor mgcDominantColor]];
    
    [self adapterIOS11];
    
    [self adapterIOS15ForMain:backButtonImage];
    
    [[UITextField appearance] setTintColor:[UIColor themeForegroundColor]];
    [[UITextView appearance] setTintColor:[UIColor themeForegroundColor]];

    
    UITabBar *tabBarAppearance = [UITabBar appearance];
    [tabBarAppearance setTranslucent:NO];
    [tabBarAppearance setTintColor:[UIColor whiteColor]];
    [tabBarAppearance setUnselectedItemTintColor:[UIColor whiteColor]];
    [tabBarAppearance setBackgroundColor:[UIColor themeTabBarColor]];

        
    [[UISwitch appearance] setOnTintColor:[UIColor mgcDominantColor]];
}

- (void)adapterIOS15ForMain:(UIImage *)image {
    if (@available(iOS 15.0, *)) {
        UINavigationBar *navigationBar = [UINavigationBar appearance];
        
        UINavigationBarAppearance *scrollEdgeAppearance = [[UINavigationBarAppearance alloc] init];
        scrollEdgeAppearance.backgroundColor = [UIColor mgcBackgroundColor1];
        navigationBar.scrollEdgeAppearance = scrollEdgeAppearance;
        scrollEdgeAppearance.shadowColor = [UIColor clearColor] ;
        scrollEdgeAppearance.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor mgcNavWordColor], NSFontAttributeName:[UIFont mediumPingFangFontOfSize:20]};
        [scrollEdgeAppearance setShadowImage:[UIImage new]];
        [scrollEdgeAppearance setBackIndicatorImage:image transitionMaskImage:image];
        [scrollEdgeAppearance setBackgroundImage:[UIImage imageNamed:@"dingbu"]];

        [UINavigationBar appearance].overrideUserInterfaceStyle = UIUserInterfaceStyleDark;

        
        UINavigationBarAppearance *standardAppearance = [[UINavigationBarAppearance alloc] init];
        standardAppearance.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor mgcNavWordColor], NSFontAttributeName:[UIFont mediumPingFangFontOfSize:20]};
        [standardAppearance setBackgroundImage:[UIImage imageNamed:@"dingbu"]];
        [standardAppearance setBackIndicatorImage:image transitionMaskImage:image];
        [standardAppearance setShadowImage:[UIImage new]];
        standardAppearance.shadowColor = [UIColor clearColor] ;
        navigationBar.standardAppearance = standardAppearance;
        

        

    }
}

- (void)adapterIOS11 {
    // 适配iOS11以上UITableview 、UICollectionView、UIScrollview 列表/页面偏移
    if (@available(iOS 11.0, *)){
        [[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];

        [[UITableView appearance] setEstimatedRowHeight:0];
        [[UITableView appearance] setEstimatedSectionFooterHeight:0];
        [[UITableView appearance] setEstimatedSectionHeaderHeight:0];
    }
}

- (UIImage *)backButtonImage {
    if (!_backButtonImage) {
        _backButtonImage = [UIImage imageNamed:@"ic_nav_back"];
        _backButtonImage = [_backButtonImage imageByInsetEdge:UIEdgeInsetsMake(0, -6, 2, 0) withColor:[UIColor clearColor]];
        _backButtonImage = [_backButtonImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    return _backButtonImage;
}

@end
